#include <bits/stdc++.h>
using namespace std;
int N, M;
const int maxn = 100+5, maxs = 1000;
int d[maxn][maxs][maxs];
vector<int> S[maxn], sta;
map<int, int> num;
inline bool can(int s){
    return ((s<<1)&s)==0 && ((s<<2)&s)==0;
}

inline bool can(int s, int ss){
    return (s&ss)==0;
}

int main() {
    cin >> N >> M;
    for (int i = 0; i < 1 << M; ++i) {
        int c = 0;
        if(can(i)){
            sta.push_back(i);
            int s = i;
            while (s) s ^= s&(-s), ++c;
            num[i] = c;
        }
    }
    for (int i = 1; i <= N; ++i) {
        int s = 0;
        for (int j = 1; j <= M; ++j) {
            char c; cin >> c;
            s <<= 1;
            s += c=='P';
        }
        for(auto ss:sta) if((s&ss)==ss)S[i].push_back(ss);
    }

    for(auto s:S[1]){
        d[1][s][0] = num[s];
        for(auto s2:S[2]){
            if(can(s, s2)) d[2][s2][s] += d[1][s][0] + num[s2];
        }
    }

    for (int r = 3; r <= N; ++r)
        for(auto s1:S[r])
            for(auto s2:S[r-1])
                for(auto s3:S[r-2])
                    if(can(s3, s2) && can(s2, s1) && can(s3, s1))
                        d[r][s1][s2] = max(d[r][s1][s2], d[r-1][s2][s3] + num[s1]);

    int ans = 0;
    for(auto s1:sta){
        for(auto s2:sta){
            ans = max(ans, d[N][s1][s2]);
        }
    }
    cout  << ans;
    return 0;
}
